var app = new Vue({
    el: "#app",
    data: {
        entity: {categoryList: []},
        categoryList: [],
        category1Id: 0,
        searchEntity: {},
        //总记录数
        total: 0,
        //页大小
        pageSize: 10,
        //当前页号
        pageNum: 1,
        ids: [],
        flag: true,
        checkAll: false
    },
    methods: {
        //查询所有的一级分类
        findAllCategory: function () {
            axios.get("../category/findAll.do").then(function (response) {
                app.categoryList = response.data;
            });
        },

        save: function () {
            //设置商品描述信息
            //设置商品描述信息
            var method = "save";
            if (this.entity.id != null) {
                //如果id存在则说明是 修改
                method = "update"
            }

            axios.post("../category/" + method + ".do", this.entity).then(function (response) {
                if (response.data.success) {
                    alert(response.data.message);
                    app.findAllCategory(1);
                } else {
                    alert(response.data.message);
                }
            });
        },

        findOne: function (id) {
            this.entity = {};
            axios.get("../category/findOne/" + id + ".do").then(function (response) {
                app.entity = response.data;
                app.flag = true
            });

        },
        deleteList: function () {
            if (this.ids.length < 1) {
                alert("请选择要删除的记录");
                return;
            }
            if (confirm("确定要删除选中的记录吗？")) {
                axios.get("../category/deleteList.do?ids=" + this.ids).then(function (response) {
                    if (response.data.success) {
                        app.findAllCategory(1);
                        app.ids = [];
                    } else {
                        alert(response.data.message);
                    }
                });
            }
        },

        //全选
        selectAll: function () {
            this.ids = [];
            if (!this.checkAll) {
                for (const cate of this.categoryList) {
                    this.ids.push(cate.id);
                }
            }
        },
        //添加分类
        addTableRow: function () {
            this.entity.categoryList.push({});
        },

        //删除分类
        deleteTableRow: function (index) {
            //删除的元素索引号，要删除的元素个数
            this.entity.categoryList.splice(index, 1);
        },

    },
    watch: {
        flag: {
            handler: function () {
                axios.get("../category/findCategoryById/" + this.entity.categoryId + ".do").then(function (response) {
                    app.category1Id = response.data.id;
                });

            }
        },
        ids: {
            deep: true,
            handler: function () {
                if (this.ids.length == this.categoryList.length && this.ids.length > 0) {
                    this.checkAll = true;
                } else {
                    this.checkAll = false;
                }
            }

        }
    },
    created: function () {
        this.findAllCategory();
    }
});